package com.mhxy.service.impl;

import com.mhxy.constant.PicConstants;
import com.mhxy.enums.Coord;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.MouseUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * 自动师门
 */
@Slf4j
public class MasterSchoolImpl extends AbstractTask {

    private static final String 师门任务列表 = PicConstants.TASK_SCHOOL_DIR + File.separator + "ShiMenRenWu_Right.PNG";
    private static final String 师门任务活动 = PicConstants.TASK_SCHOOL_DIR + File.separator + "ShiMen_HuoDong.PNG";
    private static final String 师门去完成活动 = PicConstants.TASK_SCHOOL_DIR + File.separator + "ShiMenQuWanChen_HuoDong.PNG";
    private static final String 师门活动已完成 = PicConstants.TASK_SCHOOL_DIR + File.separator + "ShiMenWanCheng_HuoDong.PNG";

    public MasterSchoolImpl(WinDef.HWND hwnd_0) {
        super(hwnd_0);
    }

    @Override
    public String getTaskName() throws Exception {
        return "自动师门";
    }

    @Override
    public Boolean before() throws Exception {
        clickEsc1();
        return goToJianYe();
    }

    @Override
    public Boolean execute() throws Exception {
        clickEsc3();

        if (!findAndClickTaskList(师门任务列表)) {
            if (isFinished()) {
                return true;
            }
            return false;
        }
        ThreadUtil.sleep1();
        FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_0, 师门任务活动, 2);
        FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_0, 师门去完成活动, 2);
        return true;
    }

    @Override
    public Boolean after() throws Exception {
        while (true) {
            if (isFinished()) {
                return true;
            }
            log.info("【{}】进行中，继续等待...", getTaskName());
            ThreadUtil.sleep(20000);
        }
    }

    private boolean isFinished(){
        try {
            // 点击活动
            if (!findAndClickActivity()) {
                return false;
            }

            if (FuzzyPicUtil.findPic(hwnd_0, 师门活动已完成, 2).isFinded()) {
                clickEsc3();
                log.info("【{}】已完成", getTaskName());
                return true;
            }
            // 拉到最顶端
            MouseUtil.dragMove(hwnd_0, Coord.ACTIVITY_WHELL.getX(), Coord.ACTIVITY_WHELL.getY(), 0, -100);

            if (FuzzyPicUtil.findPic(hwnd_0, 师门活动已完成, 2).isFinded()) {
                clickEsc3();
                log.info("【{}】已完成", getTaskName());
                return true;
            }

            // 拉到最顶端
            MouseUtil.dragMove(hwnd_0, Coord.ACTIVITY_WHELL.getX(), Coord.ACTIVITY_WHELL.getY(), 0, -500);

            if (FuzzyPicUtil.findPic(hwnd_0, 师门活动已完成, 2).isFinded()) {
                clickEsc3();
                log.info("【{}】已完成", getTaskName());
                return true;
            }
            clickEsc3();
        } catch (Throwable e){
            e.printStackTrace();
        }
        return false;
    }

}
